package org.tailor

class RegionsController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [regionsInstanceList: Regions.list(params), regionsInstanceTotal: Regions.count()]
    }

    def create = {
        def regionsInstance = new Regions()
        regionsInstance.properties = params
        return [regionsInstance: regionsInstance]
    }

    def save = {
        def regionsInstance = new Regions(params)
        if (regionsInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'regions.label', default: 'Regions'), regionsInstance.id])}"
            redirect(action: "show", id: regionsInstance.id)
        }
        else {
            render(view: "create", model: [regionsInstance: regionsInstance])
        }
    }

    def show = {
        def regionsInstance = Regions.get(params.id)
        if (!regionsInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'regions.label', default: 'Regions'), params.id])}"
            redirect(action: "list")
        }
        else {
            [regionsInstance: regionsInstance]
        }
    }
	
	def renderImage = {
		def regionsInstance = Regions.get(params.id)
		  if (regionsInstance!=null) {
			 response.setContentLength(regionsInstance.regionPicture.length)
			 byte[] image =regionsInstance.regionPicture
			  response.outputStream.write(image)
		 }
		 else {
			 response.setContentType("image/jpeg")
			 response.getOutputStream().write("No Image".bytes)
		 }
	 }

    def edit = {
        def regionsInstance = Regions.get(params.id)
        if (!regionsInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'regions.label', default: 'Regions'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [regionsInstance: regionsInstance]
        }
    }

    def update = {
        def regionsInstance = Regions.get(params.id)
		def regionPicture = regionsInstance.regionPicture
        if (regionsInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (regionsInstance.version > version) {
                    
                    regionsInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'regions.label', default: 'Regions')] as Object[], "Another user has updated this Regions while you were editing")
                    render(view: "edit", model: [regionsInstance: regionsInstance])
                    return
                }
            }
            regionsInstance.properties = params
			if(params.regionPicture.getSize() == 0){
				regionsInstance.regionPicture = regionPicture
				}
            if (!regionsInstance.hasErrors() && regionsInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'regions.label', default: 'Regions'), regionsInstance.id])}"
                redirect(action: "show", id: regionsInstance.id)
            }
            else {
                render(view: "edit", model: [regionsInstance: regionsInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'regions.label', default: 'Regions'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def regionsInstance = Regions.get(params.id)
        if (regionsInstance) {
            try {
                regionsInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'regions.label', default: 'Regions'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'regions.label', default: 'Regions'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'regions.label', default: 'Regions'), params.id])}"
            redirect(action: "list")
        }
    }
}
